package com.atguigu.spzx.service.cart.service.impl;

import com.atguigu.spzx.common.handler.exception.SpzxException;
import com.atguigu.spzx.common.handler.interceptor.SpzxAuthInterceptor;
import com.atguigu.spzx.model.common.Result;
import com.atguigu.spzx.model.common.ResultCodeEnum;
import com.atguigu.spzx.model.entity.h5.CartInfo;
import com.atguigu.spzx.model.entity.h5.UserInfo;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.product.client.feign.ProductFeignClient;
import com.atguigu.spzx.service.cart.service.CartService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * title:
 * author: zgl
 * date: 2024/1/17
 * description：
 */
@Service
public class CartServiceImpl implements CartService {
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ProductFeignClient productFeignClient;


    //获取userId

    @Override
    public void addToCart(Long skuId, Integer skuNum) {
        //获取userId
        UserInfo userInfo = SpzxAuthInterceptor.threadLocal.get();
        CartInfo cartInfo = null;
        //初始化购物车
        BoundHashOperations bho = getBho();
        //存在同类
        if (bho.hasKey(skuId.toString())) {
            cartInfo = (CartInfo) bho.get(skuId.toString());
            cartInfo.setSkuNum(cartInfo.getSkuNum() + skuNum);
            cartInfo.setUpdateTime(new Date());
            bho.put(skuId.toString(), cartInfo);

        } else {
            //不存在同类
            cartInfo = new CartInfo();
            cartInfo.setSkuNum(skuNum);
            cartInfo.setIsChecked(1);
            cartInfo.setUserId(userInfo.getId());
            cartInfo.setCreateTime(new Date());
            cartInfo.setSkuId(skuId);
            Result<ProductSku> result = productFeignClient.getProductList(skuId);
            ProductSku productSku = result.getData();
            if (result.getCode() != 200) {
                throw new SpzxException(ResultCodeEnum.FAIL);
            }
            cartInfo.setCartPrice(productSku.getSalePrice());
            cartInfo.setImgUrl(productSku.getThumbImg());
            cartInfo.setSkuName(productSku.getSkuName());
            bho.put(skuId.toString(), cartInfo);
        }
    }

    private BoundHashOperations getBho() {
        UserInfo userInfo = SpzxAuthInterceptor.threadLocal.get();
        return redisTemplate.boundHashOps("spzx:user:cart" + userInfo.getId());
    }

    @Override
    public List<CartInfo> cartList() {
        BoundHashOperations bho = getBho();
        return bho.values();
    }

    @Override
    public void deleteCart(Long skuId) {
        BoundHashOperations bho = getBho();
        CartInfo cartInfo = (CartInfo) bho.get(skuId.toString());
        if (cartInfo.getIsChecked() == 1) {
            bho.delete(skuId.toString());
        }
    }

    @Override
    public void checkCart(Long skuId, Integer isChecked) {
        BoundHashOperations bho = getBho();
        CartInfo cartInfo = (CartInfo) bho.get(skuId.toString());
        cartInfo.setIsChecked(isChecked);
        bho.put(skuId.toString(), cartInfo);
    }


    @Override
    public void allCheckCart(Integer isChecked) {
        BoundHashOperations bho = getBho();
        if (bho.size() == 0) {
            return;
        }
        bho.entries().forEach((key, value) -> {
            CartInfo cartInfo = (CartInfo) value;
            cartInfo.setIsChecked(isChecked);
            bho.put(key, cartInfo);
        });
    }

    @Override
    public void clearCart() {
        UserInfo userInfo = SpzxAuthInterceptor.threadLocal.get();
        redisTemplate.delete("spzx:user:cart" + userInfo.getId());
    }


    @Override
    public List<CartInfo> getCheckedList() {
        BoundHashOperations bho = getBho();
        List<CartInfo> values = bho.values();
        List<CartInfo> checkedList = values.stream().filter(cartInfo -> cartInfo.getIsChecked() == 1).collect(Collectors.toList());
        System.out.println(checkedList);
        return checkedList;
    }

    @Override
    public void deleteChecked() {
        BoundHashOperations<String, String, CartInfo> bho = getBho();
        bho.entries().forEach((key, value) -> {
            if (value.getIsChecked() == 1) {
                bho.delete(key);
            }
        });
    }
}
